#! /bin/bash

# ------------------------------------------
#
#       https://github.com/Statemood/monitor
# ------------------------------------------

# Data read from /proc/diskstats

# Description:
# The /proc/diskstats file displays the I/O statistics
# of block devices. Each line contains the following 14 fields:
#	 1 - major number
#	 2 - minor mumber
#	 3 - device name
#	 4 - reads completed successfully
#	 5 - reads merged
#	 6 - sectors read
#	 7 - time spent reading (ms)
#	 8 - writes completed
#	 9 - writes merged
#	10 - sectors written
#	11 - time spent writing (ms)
#	12 - I/Os currently in progress
#	13 - time spent doing I/Os (ms)
#	14 - weighted time spent doing I/Os (ms)
#	For more details refer to https://www.kernel.org/doc/Documentation/iostats.txt

for arg in "$@"
do
    case $arg in
        --disk)
            disk="$2"
            shift 2
            ;;
        --key)
            case $2 in
                reads_completed)        num=4   ;;
                reads_merged)           num=5   ;;
                sectors_read)           num=6   ;;
                time_spent_reading)     num=7   ;;
                writes_completed)       num=8   ;;
                writes_merged)          num=9   ;;
                sectors_written)        num=10  ;;
                time_spent_writing)     num=11  ;;
                io_currently_progress)  num=12  ;;
                time_spent_io)          num=13  ;;
                weighted_time_spent_io) num=14  ;;
            esac
            ;;
    esac
done

grep "[hs]d[a-z]" /proc/diskstats | grep "$disk " | awk "{print \$$num}"
